jQuery.fn.extend({
    uploadPreview: function (opts) {
        var _self = this,
            _this = $(this);
        opts = jQuery.extend({
            Img: "ImgPr",
            Width: 0,
            Height: 0,
            ImgType: ["gif", "jpeg", "jpg", "bmp", "png"],
            size:300,
            Callback: function () {
            }
        }, opts || {});
        _self.getObjectURL = function (file) {
            var url = null;
            if (window.createObjectURL != undefined) {
                url = window.createObjectURL(file)
            } else if (window.URL != undefined) {
                url = window.URL.createObjectURL(file)
            } else if (window.webkitURL != undefined) {
                url = window.webkitURL.createObjectURL(file)
            }
            return url
        };
        _this.change(function () {
            if (this.value) {
                if (!RegExp("\.(" + opts.ImgType.join("|") + ")$", "i").test(this.value.toLowerCase())) {
                   //alert("选择文件错误,图片类型必须是" + opts.ImgType.join("，") + "中的一种");
                    method.msg_layer({
                        title: "提示信息",
                        content:"选择文件错误,图片类型必须是" + opts.ImgType.join("，") + "中的一种",
                        btn: ["确定",""]
                    });
                    this.value = "";
                    return false
                }
                /*限制图片大小*/
                if(this.files[0].size > opts.size*1024){
                    method.msg_layer({
                        title: "提示信息",
                        content:"当前图片大小为 "+Math.floor(this.files[0].size/1024)+'kb,请上传'+opts.size+'kb以内图片',
                        btn: ["确定",""]
                    });
                    return false;
                   // alert("当前图片大小为 "+Math.floor(this.files[0].size/1024)+'kb,请上传'+opts.size+'kb以内图片')
                }
                /*限制图片尺寸*/
                var bok=true;
                if ($.browser){
                    try {
                        var img=new Image();
                        $(img).attr("src",_self.getObjectURL(this.files[0]));
                        var this1=this;
                        img.onload=function () {
                            if(img.width>600 ||img.width<500 ||img.height>368 || img.height<268 ){
                               // alert("图片宽度为"+img.width+'px,' +"图片高度为"+img.height+'px,'+"要求上传750px*390px的图片");
                                method.msg_layer({
                                    title: "提示信息",
                                    content:"图片宽度为"+img.width+'px,' +"图片高度为"+img.height+'px,'+"要求上传750px*390px的图片",
                                    btn: ["确定",""]
                                });

                                bok=false;
                                console.log(222)

                            }
                            if(bok){
                                $(opts.Img).attr('src', _self.getObjectURL(this1.files[0]))
                            }

                        };
                    } catch (e) {
                        var src = "";
                        var obj = $("#" + opts.Img);
                        var div = obj.parent("div")[0];
                        _self.select();
                        if (top != self) {
                            window.parent.document.body.focus()
                        } else {
                            _self.blur()
                        }
                        src = document.selection.createRange().text;
                        document.selection.empty();
                        obj.hide();
                        obj.parent("div").css({
                            'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)',
                            'width': opts.Width + 'px',
                            'height': opts.Height + 'px'
                        });
                        div.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = src
                    }
                }else {

                           var img=new Image();
                           var this1=this
                         /*  img.onload=function () {
                                if(img.width >=650 ||img.height >=464 ){
                                    method.msg_layer({
                                        title: "提示信息",
                                        content:"图片宽度为"+img.width+'px,' +"图片高度为"+img.height+'px,'+"要求上传750px*390px的图片",
                                        btn: ["确定",""]
                                    });
                                    bok=false;
                                }
                               if(bok){
                                    $(opts.Img).attr('src', _self.getObjectURL(this1.files[0]))
                               }
                           };*/
                    img.onload=function () {
                        if(img.width>600 ||img.width<500 ||img.height>368 || img.height<268 ){
                            // alert("图片宽度为"+img.width+'px,' +"图片高度为"+img.height+'px,'+"要求上传750px*390px的图片");
                            method.msg_layer({
                                title: "提示信息",
                                content:"图片宽度为"+img.width+'px,' +"图片高度为"+img.height+'px,'+"要求上传750px*390px的图片",
                                btn: ["确定",""]
                            });
                            console.log(111)
                            bok=false;

                        }
                        if(bok){
                            console.log(888)
                            $("#" + opts.Img).attr('src', _self.getObjectURL(this1.files[0]))
                        }

                    };

                   // $(img).attr("src",_self.getObjectURL(this.files[0]));
                }
                opts.Callback()
            }
        })
    }
});